package org.argeproje.resim;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.argeproje.resim.ui.model.ShapesDiagram;
import org.argeproje.resim.ui.viewers.ImageView;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartConstants;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.editor.*;
import org.eclipse.ui.part.MultiPageSelectionProvider;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.SelectionManager;
import org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;

public class ResimProjectEditor extends FormEditor {

	private List<ShapesEditor> _shapesEditorList;
	public static final String EDITOR_ID = "org.argeproje.resim.editors.resimprojecteditor";
			
	public ResimProjectEditor()
	{		
		super();
		_shapesEditorList = new ArrayList<ShapesEditor>();
	}
		
	public static ResimProjectEditor getActiveProjectEditor()
	{
		IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	    IWorkbenchPage workbenchPage = workbenchWindow.getActivePage();
	    return (ResimProjectEditor)workbenchPage.getActiveEditor();	    
	}
	
	@Override
	protected void addPages() {
		// TODO Auto-generated method stub
		try {
			//addPage(new ShapeEditorPage(this,EDITOR_ID,"Editor"));
			ShapesEditor shapesEditor = new ShapesEditor();
			//shapesEditor.setInput(getEditorInput());
			int pageIndex = addPage(shapesEditor,getEditorInput());
			
			setPageText(pageIndex,"Editor");
			_shapesEditorList.add(shapesEditor);
		} catch (PartInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	    * @see org.eclipse.ui.part.EditorPart#doSaveAs()
	    */
	   @Override
	   public void doSaveAs() {/* Not allowed */}

	   /*
	    * @see org.eclipse.ui.part.EditorPart#getEditorInput()
	    */
	   @Override
	   public ShapesEditorInput getEditorInput() {
	      return (ShapesEditorInput) super.getEditorInput();
	   }
	   
	   /*
	    * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
	    */
	   @Override
	   public boolean isSaveAsAllowed() {
	      return false;
	   }

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub
		((ShapesEditor)getActiveEditor()).doSave(monitor);
	}
	
	@Override
	public int addPage(IFormPage page) throws PartInitException {
		int i = super.addPage(page.getPartControl());
		configurePage(i, page);
		return i;
	}
		
	public int addPage(IFormPage page,int style) {
		int index = getPageCount();
		addPage(index,page.getPartControl(),style);
		
		try {
			configurePage(index,page);			
		} catch (PartInitException e) {
			// cannot happen for controls
		}
		
		return index;
	}
	
	public void addPage(int index, Control control,int style) {
		createItem(index, control,style);
	}
	
	private CTabItem createItem(final int index, Control control,int style) {
		CTabItem item = new CTabItem((CTabFolder)getContainer(), style, index);
		item.setControl(control);
		item.addDisposeListener(new DisposeListener(){
			public void widgetDisposed(DisposeEvent de) {				
				pages.remove(index);
			}
		});
		return item;
	}
	
	public IEditorPart getEditorPart( )
	{
		return this;
	}	
	
	/*
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		IEditorPart activeEditor = getSite().getPage().getActiveEditor();
		System.out.println(activeEditor.getClass().getName());
		//if (this.equals(activeEditor))
		//updateActions(getSelectionActions());
	}*/

	
	public Object getAdapter( Class type )
	{
		//System.out.println(super.getAdapter(type).getClass().getName());
		return super.getAdapter(type);
	}
}
